package com.dp.searchbuilder.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dp.searchbuilder.bean.BuildEvent;
import com.dp.searchbuilder.dao.BuilderDao;
import com.ibatis.sqlmap.client.SqlMapClient;

public class BuilderDaoImpl implements BuilderDao{

	private SqlMapClient sqlMapClient;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(BuilderDaoImpl.class); 
	@Override
	public BuildEvent getSingleEvent(long id) {
		BuildEvent buildEvent = null;
		try 
		{
			buildEvent = (BuildEvent)sqlMapClient.queryForObject("BuildEvent.selectSingleEvent", id);
		} 
		catch (SQLException e) 
		{
			LOGGER.error("get single event failed", e);
		}
		return buildEvent;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BuildEvent> getAllEvent() {
		String param = "";
		List<BuildEvent> evnetList = new ArrayList<BuildEvent>();
		try {
			evnetList = (List<BuildEvent>)sqlMapClient.queryForList("BuildEvent.selectAllEvents",param);
		} catch (SQLException e) {
			LOGGER.error("get all event failed", e);
		}
		return evnetList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BuildEvent> getCertainStatusEvent(String eventStatus) {
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("buildStatus", eventStatus);
		List<BuildEvent> evnetList = new ArrayList<BuildEvent>();
		try {
			evnetList = (List<BuildEvent>)sqlMapClient.queryForList("BuildEvent.selectStatusEvents", paramMap);
		} catch (SQLException e) {
			LOGGER.error("get certain event failed", e);
		}
		return evnetList;
	}

	@Override
	public int insertBuildEvent(BuildEvent buildEvent) {
		// TODO Auto-generated method stub
		try {
			 return (Integer)sqlMapClient.insert("BuildEvent.insertEvent", buildEvent);
		} catch (SQLException e) {
			LOGGER.error("insert event failed", e);
		}
		return -1;
		
	}

	@Override
	public void updateEvent(int id ,String buildStatus) {
		// TODO Auto-generated method stub
		Map<String,Object> paramMap = new HashMap<String, Object>();
		paramMap.put("id", id);
		paramMap.put("buildStatus", buildStatus);
		
		try {
			sqlMapClient.update("BuildEvent.updateBuildEvent", paramMap);
		} catch (SQLException e) {
			LOGGER.error("update event failed", e);
		}
		
	}
	
	@Override
	public List<BuildEvent> getCertainEvent(String gitServer, String branchName) {
		Map<String,String> paramMap = new HashMap<String, String>();
		paramMap.put("gitServer", gitServer);
		paramMap.put("branchName", branchName);
		List<BuildEvent> evnetList = null;
		try {
			evnetList = sqlMapClient.queryForList("BuildEvent.selectCertainEvent", paramMap);
		} catch (SQLException e) {
			LOGGER.error("query certain event failed", e);
		}
		return evnetList;
	}
	

	public SqlMapClient getSqlMapClient() {
		return sqlMapClient;
	}

	public void setSqlMapClient(SqlMapClient sqlMapClient) {
		this.sqlMapClient = sqlMapClient;
	}


	

}
